<%--
  Created by IntelliJ IDEA.
  User: jun
  Date: 2018/12/21
  Time: 11:18 AM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传文件</title>
    <style>
    </style>
</head>
<input type="file" multiple="multiple" class="j_files">
<input type="button" value="上传作业文件" class="j_upload_file">
<body>
<script src="${pageContext.request.contextPath}/js/common/jquery.1.11.3.min.js"></script>
<script src="${pageContext.request.contextPath}/js/common/moment.min.js"></script>
<script type="text/javascript">
    var GData = {};
    var GFunc = {
        bind: function () {
            $('.j_upload_file').click(function () {
                GFunc.uploadFile();
            });
        },
        //上传原文接口
        uploadFile: function () {
            try {
                var fileObjArr = $('.j_files').get(0).files;
                var fd = new FormData();
                for (var u = 0; u < fileObjArr.length; u++) {
                    var fileObj = fileObjArr[u];
                    if (fileObj) {
                        fd.append("files", fileObj);
                    }
                }
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "/file/upload_homework_file.do");
                xhr.onload = function () {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var data = JSON.parse(xhr.responseText);
                            if (data.code == 200) {
                                alert("上传成功：" + data.message);
                            } else {
                                alert("上传失败：" + data.message);
                                return false;
                            }
                        }
                    } else {
                        alert('上传失败');
                    }
                };
                xhr.send(fd);
            } catch (e) {
                alert('上传原文异常：' + e.message);
                return;
            }
        },
    };
    $(function () {
        GFunc.bind();
    });
</script>
</body>
</html>
